package uestc.bitman.edxapp.downloaded;

import uestc.bitman.edxapp.EdxAppConfigurations;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.mainui.EdxAppFragment;
import uestc.bitman.edxapp.player.WebViewPlayerActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;

public class DownloadedFragment extends EdxAppFragment {

	private ExpandableListView mExpandableListView;

	private VideoManager mVideoManager;

	public DownloadedFragment() {

	}

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.downloaded_fragment, container,
				false);

		mVideoManager = new VideoManager(getActivity(),
				Environment.getExternalStorageDirectory()
						+ EdxAppConfigurations.DOWNLOAD_PATH);
		mExpandableListView = (ExpandableListView) view
				.findViewById(R.id.downloaded_listview);
		mExpandableListView.setAdapter(mVideoManager);

		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int parentPos, int childPos, long id) {
				if (v.getTag() != null) {
					String nameString = (String) v.getTag();
					GetVideo getVideo = new GetVideo(Environment
							.getExternalStorageDirectory()
							+ EdxAppConfigurations.DOWNLOAD_PATH);
					String path = getVideo.getVideoPath(nameString);

					// launch the player
					Intent intent = new Intent(getActivity(),
							WebViewPlayerActivity.class);
					intent.putExtra("video",
							Environment.getExternalStorageDirectory()
									+ EdxAppConfigurations.DOWNLOAD_PATH + path);
					intent.putExtra("title", nameString);
					intent.putExtra("local", true);
					startActivity(intent);
				}
				return true;
			}
		});

		mExpandableListView
				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {
						ExpandableListContextMenuInfo myContextMenuInfo = (ExpandableListContextMenuInfo) menuInfo;
						View target = myContextMenuInfo.targetView;
						if (target.getTag() == null
								|| target.getTag() == "parent") {

						} else {
							menu.add(0, 0, 0, getString(R.string.label_play));
							menu.add(1, 1, 1, getString(R.string.label_delete));
						}
					}
				});

		return view;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id = item.getItemId();
		ExpandableListContextMenuInfo myContextMenuInfo = (ExpandableListContextMenuInfo) item
				.getMenuInfo();
		View targetView = myContextMenuInfo.targetView;

		if (id == 0) {

			// Toast.makeText(getActivity(), "play", Toast.LENGTH_SHORT).show();
			String nameString = (String) targetView.getTag();
			GetVideo getVideo = new GetVideo(
					Environment.getExternalStorageDirectory()
							+ EdxAppConfigurations.DOWNLOAD_PATH);
			String path = getVideo.getVideoPath(nameString);

			// launch the player
			Intent intent = new Intent(getActivity(),
					WebViewPlayerActivity.class);
			intent.putExtra("video", Environment.getExternalStorageDirectory()
					+ EdxAppConfigurations.DOWNLOAD_PATH + path);
			intent.putExtra("title", nameString);
			intent.putExtra("local", true);
			startActivity(intent);

		} else if (id == 1) {

			String nameString = (String) targetView.getTag();

			SetVideo setVideo = new SetVideo(
					Environment.getExternalStorageDirectory()
							+ EdxAppConfigurations.DOWNLOAD_PATH);
			GetVideo getVideo = new GetVideo(
					Environment.getExternalStorageDirectory()
							+ EdxAppConfigurations.DOWNLOAD_PATH);
			setVideo.deleteVideo(Environment.getExternalStorageDirectory()
					+ EdxAppConfigurations.DOWNLOAD_PATH,
					getVideo.getVideoPath(nameString));
			mVideoManager.notifyDataSetChanged();
		}
		return true;
	}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return false;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    }
}
